Class pgPlayerController extends KFPlayerController;

simulated static final function int FixedTurn( int current, int desired, int deltaRate )
{
	if( deltaRate==0 )
		return (current & 65535);
	current = current & 65535;
	desired = desired & 65535;

	if (current > desired)
	{
		if (current - desired < 32768)
			return current - Min((current - desired), deltaRate);
		else
			return current + Min((desired + 65536 - current), deltaRate);
	}
	else
	{
		if (desired - current < 32768)
			return current + Min((desired - current), deltaRate);
		else
			return current - Min((current + 65536 - desired), deltaRate);
	}
}

// Smooth un-roll for when exiting portals.
function UpdateRotation(float DeltaTime, float maxPitch)
{
	local rotator newRotation, ViewRotation;

	if ( bInterpolating || ((Pawn != None) && Pawn.bInterpolating) )
	{
		ViewShake(deltaTime);
		return;
	}

	// Added FreeCam control for better view control
	if (bFreeCam )
	{
		if( bFreeCamZoom )
			CameraDeltaRad += DeltaTime * 0.25 * aLookUp;
		else if( bFreeCamSwivel )
		{
			CameraSwivel.Yaw += 16.0 * DeltaTime * aTurn;
			CameraSwivel.Pitch += 16.0 * DeltaTime * aLookUp;
		}
		else
		{
			CameraDeltaRotation.Yaw += 32.0 * DeltaTime * aTurn;
			CameraDeltaRotation.Pitch += 32.0 * DeltaTime * aLookUp;
		}
	}
	else
	{
		ViewRotation = Rotation;

		// Ensure we are not setting the pawn to a rotation beyond its desired
		if( ViewRotation.Roll!=0 )
			ViewRotation.Roll = FixedTurn(ViewRotation.Roll,0,65535*DeltaTime);

		DesiredRotation = ViewRotation; //save old rotation

		TurnTarget = None;
		bRotateToDesired = false;
		bSetTurnRot = false;
		ViewRotation.Yaw += 32.0 * DeltaTime * aTurn;
		ViewRotation.Pitch += 32.0 * DeltaTime * aLookUp;

		if( (Pawn != none ) && (Pawn.Weapon != none) )
			ViewRotation = RecoilHandler(ViewRotation, DeltaTime);
		if (Pawn != None)
			ViewRotation.Pitch = Pawn.LimitPitch(ViewRotation.Pitch,DeltaTime);

		SetRotation(ViewRotation);

		ViewShake(deltaTime);
		ViewFlash(deltaTime);

		NewRotation = ViewRotation;

		if ( !bRotateToDesired && (Pawn != None) && (!bFreeCamera || !bBehindView) )
			Pawn.FaceRotation(NewRotation, deltatime);
	}
}
